package com.ruoyi.gams.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.gams.domain.GamsBusinessQueue;
import com.ruoyi.gams.domain.GamsUser;
import com.ruoyi.gams.domain.GamsWindow;
import com.ruoyi.gams.mapper.GamsBusinessQueueMapper;
import com.ruoyi.gams.mapper.GamsWindowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gams.mapper.GamsUserQueueMapper;
import com.ruoyi.gams.domain.GamsUserQueue;
import com.ruoyi.gams.service.IGamsUserQueueService;

/**
 * 排队Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-03
 */
@Service
public class GamsUserQueueServiceImpl implements IGamsUserQueueService
{
    @Autowired
    private GamsUserQueueMapper gamsUserQueueMapper;

    @Autowired
    private GamsBusinessQueueMapper businessQueueMapper;

    @Autowired
    private GamsWindowMapper windowMapper;

    /**
     * 查询排队
     *
     * @param id 排队主键
     * @return 排队
     */
    @Override
    public GamsUserQueue selectGamsUserQueueById(Integer id)
    {
        return gamsUserQueueMapper.selectGamsUserQueueById(id);
    }

    /**
     * 查询排队列表
     *
     * @param gamsUserQueue 排队
     * @return 排队
     */
    @Override
    public List<GamsUserQueue> selectGamsUserQueueList(GamsUserQueue gamsUserQueue)
    {
        return gamsUserQueueMapper.selectGamsUserQueueList(gamsUserQueue);
    }

    @Override
    public List<GamsUserQueue> selectGamsUserQueueListDesc(GamsUserQueue gamsUserQueue)
    {
        return gamsUserQueueMapper.selectGamsUserQueueListDesc(gamsUserQueue);
    }

    /**
     * 新增排队
     *
     * @param gamsUserQueue 排队
     * @return 结果
     */
    @Override
    public int insertGamsUserQueue(GamsUserQueue gamsUserQueue)
    {
        gamsUserQueue.setCreateTime(DateUtils.getNowDate());
        return gamsUserQueueMapper.insertGamsUserQueue(gamsUserQueue);
    }

    /**
     * 修改排队
     *
     * @param gamsUserQueue 排队
     * @return 结果
     */
    @Override
    public int updateGamsUserQueue(GamsUserQueue gamsUserQueue)
    {
        return gamsUserQueueMapper.updateGamsUserQueue(gamsUserQueue);
    }

    /**
     * 批量删除排队
     *
     * @param ids 需要删除的排队主键
     * @return 结果
     */
    @Override
    public int deleteGamsUserQueueByIds(Integer[] ids)
    {
        return gamsUserQueueMapper.deleteGamsUserQueueByIds(ids);
    }

    /**
     * 删除排队信息
     *
     * @param id 排队主键
     * @return 结果
     */
    @Override
    public int deleteGamsUserQueueById(Integer id)
    {
        return gamsUserQueueMapper.deleteGamsUserQueueById(id);
    }

    @Override
    public GamsUserQueue getNumber(GamsUser user, Integer businessQueueId)
    {
        GamsUserQueue userQueue = new GamsUserQueue();
        userQueue.setUserId(user.getId());
        userQueue.setUserName(user.getPartyname());
        userQueue.setStatus(1);

        GamsBusinessQueue businessQueue = businessQueueMapper.selectGamsBusinessQueueById(businessQueueId);
        if (businessQueue == null)
        {
            throw new RuntimeException("找不到业务队列");
        }


        //前缀
        String numberPrefix = businessQueue.getNumberPrefix();
        Integer numberLength = businessQueue.getNumberLength();
        //默认0号
        String lastNumber = numberPrefix + String.format("%0" + numberLength + "d", 0);
        //
        GamsUserQueue lastUserQueue = gamsUserQueueMapper.findFirstByBussinessIdOrderByIdDesc(businessQueue.getId());
        if (lastUserQueue != null)
        {
            lastNumber = lastUserQueue.getNumber();
        }
        String num = lastNumber.replace(numberPrefix, "");// no.substring(2)去掉前两个字符
        String str = String.format("%0" + numberLength + "d", Integer.parseInt(num) + 1);//如果小于6位左边补0
        String number = numberPrefix + str;
        userQueue.setNumber(number);
        userQueue.setNumberGetmethod(1);
        Integer queueUserCount = gamsUserQueueMapper.countByBussinessIdAndStatus(businessQueue.getId(), 1);
        userQueue.setQueueUserCount(queueUserCount);
        userQueue.setCreateTime(new Date());
        userQueue.setUpdateTime(new Date());
        userQueue.setBussinessId(businessQueue.getId());
        userQueue.setBussinessName(businessQueue.getBusinessName());
        userQueue.setUserName(user.getName());
        gamsUserQueueMapper.insertGamsUserQueue(userQueue);
        return userQueue;
    }

    @Override
    public void findTodayUserQueue(String windowName)
    {
        GamsWindow window = windowMapper.findFirstByName(windowName);
        if (window == null)
        {
            throw new GlobalException("窗口不存在:" + windowName);
        }
        String[] ids = window.getBusinessQueueIds().split(",");

    }
}
